package com.iwhalecloud.citybrain.flow.platform.manager.application.event;

import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.EventExceptionDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowEventExceptionRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
　　* @description: 事件异常处理器
　　* @author shenrm
　　* @date 2022/5/9 21:22
　　*/
@Slf4j
@Service
public class EventExceptionProcessor {

    /**
     * 事件异常数据持久化
     */
    @Autowired
    private FlowEventExceptionRepository flowEventExceptionRepository;

    /**
     * 记录事件执行时的异常信息
     *
     * @param exceptionMsg 异常信息
     * @param requestUrl 请求URL
     * @param requestBody 请求参数
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Exception.class)
    public void insertEventExceptionInfo(String exceptionMsg, String requestUrl, String requestBody) {
        try {
            // 为空时,对象实例化
            if (Objects.isNull(flowEventExceptionRepository)){
                flowEventExceptionRepository = SpringUtils.getBean(FlowEventExceptionRepository.class);
            }
            EventExceptionDomain eventExceptionDomain = this.convertToEventExceptionDomain(exceptionMsg, requestUrl, requestBody);
            // 新增异常信息
            flowEventExceptionRepository.insertEventException(eventExceptionDomain);
        } catch (Exception ex) {
            log.error("EventExceptionProcessor insertEventException Error:",ex);
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Exception.class)
    public void insertEventExceptionInfo(String exceptionMsg, String requestUrl, String requestBody, String processInstanceId, String nodeCode, String callMethod, String modelId, String modelKey, String modelName, String name, String deploymentId, String callbackType, String eventType) {
        try {
            // 为空时,对象实例化
            if (Objects.isNull(flowEventExceptionRepository)){
                flowEventExceptionRepository = SpringUtils.getBean(FlowEventExceptionRepository.class);
            }
            EventExceptionDomain eventExceptionDomain = this.convertToEventExceptionDomain(exceptionMsg, requestUrl, requestBody,processInstanceId,nodeCode,callMethod,modelId,modelKey,modelName,name,deploymentId,callbackType,eventType);
            // 新增异常信息
            flowEventExceptionRepository.insertEventException(eventExceptionDomain);
        } catch (Exception ex) {
            log.error("EventExceptionProcessor insertEventException Error:",ex);
        }
    }

    public EventExceptionDomain convertToEventExceptionDomain(String exceptionMsg, String requestUrl, String requestBody,String processInstanceId,String nodeCode,String callMethod,String modelId,String modelKey,String modelName,String name,String deploymentId,String callbackType,String eventType) {
        EventExceptionDomain eventException = new EventExceptionDomain();

        // 新的请求url,替换参数之后的
        eventException.setCallbackContent(requestUrl);

        // 请求的参数
        eventException.setRequestBody(requestBody);

        // 异常信息
        eventException.setExceptionMsg(exceptionMsg);

        //流程实例id
        eventException.setProcessInstanceId(processInstanceId);

        //节点编码
        eventException.setNodeCode(nodeCode);
        //同步、异步
        eventException.setCallMethod(callMethod);

        eventException.setModelId(modelId);

        eventException.setModelKey(modelKey);

        eventException.setModelName(modelName);
        //设置事件类型 前置、后置
        eventException.setEventType(eventType);

        eventException.setName(name);

        eventException.setDeploymentId(deploymentId);

        eventException.setCallbackType(callbackType);
        return eventException;
    }
    /**
     * 组装对象
     *
     * @param exceptionMsg 异常信息
     * @param requestUrl 请求URL
     * @param requestBody 请求参数
     * @return
     */
    public EventExceptionDomain convertToEventExceptionDomain(String exceptionMsg, String requestUrl, String requestBody) {
        EventExceptionDomain eventException = new EventExceptionDomain();

        // 新的请求url,替换参数之后的
        eventException.setCallbackContent(requestUrl);

        // 请求的参数
        eventException.setRequestBody(requestBody);

        // 异常信息
        eventException.setExceptionMsg(exceptionMsg);
        return eventException;
    }
}
